package com.winway.framework.sap.util;


import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.winway.framework.sap.config.SapConn;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * 连接sap的工具类
 *
 * @author yhf
 * @date 2022-04-18
 */
@Slf4j
public class SapConnUtils {

    /**
     * 所属异构系统，需要sap提供
     */
    private static final String ABAP_AS_POOLED = "SAP_RFC";

    /**
     * sap连接文件的后缀名
     */
    private static final String DESTINATION_SUFFIX = ".jcoDestination";

    /**
     * 初始化SAP连接
     */
    private static void initProperties() {
        Properties connectProperties = new Properties();
        // SAP服务器
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, SapConn.JCO_ASHOST);
        // SAP系统编号
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, SapConn.JCO_SYSNR);
        // SAP集团
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, SapConn.JCO_CLIENT);
        // SAP用户名
        connectProperties.setProperty(DestinationDataProvider.JCO_USER, SapConn.JCO_USER);
        // SAP密码
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, SapConn.JCO_PASSWD);
        // SAP登录语言
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG, SapConn.JCO_LANG);
        // 最大连接数
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, SapConn.JCO_POOL_CAPACITY);
        // 最大连接线程
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, SapConn.JCO_PEAK_LIMIT);
        // SAP ROUTER
        connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, SapConn.JCO_SAPROUTER);
        // 创建SAP接口属性文件。
        createDataFile(connectProperties);
    }

    /**
     * 创建SAP接口属性文件。
     *
     * @param properties 属性文件内容
     */
    private static void createDataFile(Properties properties) {
        File cfg = new File(ABAP_AS_POOLED + DESTINATION_SUFFIX);
        if (cfg.exists()) {
            cfg.deleteOnExit();
        }
        try {
            FileOutputStream fos = new FileOutputStream(cfg, false);
            // 把集合中的临时数据，持久写入到硬盘中
            properties.store(fos, "for tests only !");
            fos.close();
        } catch (Exception e) {
            log.error("Sap创建资源文件失败，错误信息：{}", e.getMessage());
        }
    }

    /**
     * 获取SAP连接
     *
     * @return SAP连接对象
     */
    public static JCoDestination connect() {
        log.info("建立SAP连接...");
        initProperties();
        try {
            JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
            destination.ping();
            log.info("已成功建立sap的连接");
            return destination;
        } catch (JCoException e) {
            log.error("建立SAP连接失败，错误信息：{}", e.getMessage());
            throw new RuntimeException();
        }
    }
}
